<?php
class News extends Model {
	public function addCate($name,$status,$time) {
		return $this->dbo->query("INSERT INTO news_category (name,status,created) VALUES ('".$this->dbo->escape($name)."','".$this->dbo->escape($status)."','".$time."')");
	}
	public function getAllNewsCate() {
		$result = $this->dbo->query("SELECT * FROM news_category ORDER BY id");
		return $result->rows;
	}
	public function getNewsCateById($cate_id) {
		$result = $this->dbo->query("SELECT * FROM news_category WHERE id = '".(int)$cate_id."' ORDER BY id");
		return $result->row;
	}
	public function updateCate($cate_id,$name,$status) {
		return $this->dbo->query("UPDATE news_category SET name='".$this->dbo->escape($name)."',status = '".$this->dbo->escape($status)."' WHERE id = '".(int)$cate_id."'");
	}
	public function delNewsCateById($id) {
		return $this->dbo->query("DELETE FROM news_category WHERE id='".(int)$id."'");
	}
	public function addNews($data) {
		return $this->dbo->query("INSERT INTO news (cate_id,title,short_desc,description,created) VALUES ('".$this->dbo->escape($data['cate_id'])."','".$this->dbo->escape($data['title'])."','".$this->dbo->escape($data['short_desc'])."','".$this->dbo->escape($data['description'])."','".$data['created']."')");
	}
	public function getAllNews($start=0,$limit=10) {
		$result = $this->dbo->query("SELECT news.*,news_category.name FROM news INNER JOIN news_category ON news_category.id = news.cate_id ORDER BY created LIMIT $start,$limit");
		return $result->rows;
	}
	public function delNewsById($id) {
		return $this->dbo->query("DELETE FROM news WHERE id='".(int)$id."'");
	}
	public function updateNews($data) {
		return $this->dbo->query("UPDATE news SET cate_id='".$this->dbo->escape($data['cate_id'])."',title='".$this->dbo->escape($data['title'])."',short_desc='".$this->dbo->escape($data['short_desc'])."',description='".$this->dbo->escape($data['description'])."' WHERE id = '".(int)$data['news_id']."'");
	}
	public function getNewsById($id) {
		$result = $this->dbo->query("SELECT news.*,news_category.name FROM news INNER JOIN news_category ON news_category.id = news.cate_id WHERE news.id = '".(int)$id."'");
		return $result->row;
	}
	public function getNewsByCateId($category_id,$offset=0,$limit=10){
		$result = $this->dbo->query("SELECT * FROM news WHERE cate_id='".(int)$category_id."' ORDER BY created LIMIT $offset,$limit");
		return $result->rows;
	}
	
	public function getNewsCountByCateId($category_id){
		$result = $this->dbo->query("SELECT count(*) as total FROM news WHERE cate_id='".(int)$category_id."'");
		return $result->row['total'];
	}
	public function getAllNewsCount() {
		$result = $this->dbo->query("SELECT count(*) as total FROM news");
		return $result->row['total'];
	}
}
?>